package ypmits.components.objectdivider {
	import ypmits.components.Component;
	import ypmits.components.ComponentSkin;
	import ypmits.components.IComponent;
	import ypmits.components.objectdivider.divider.HDivider;

	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.events.Event;

	/**
	 * @author Dave Lenz
	 */
	public class ObjectDivider extends Component implements IComponent {

		private var background : Shape;
		private var rowdividers : Array = [];
		private var rowobjects : Array = [];
		private var rowdividerheight : Number;
		private var columndividerwidth : Number;
		private var currentchangeobjecta : DisplayObject;
		private var currentchangeobjectb : DisplayObject;
		private var currentdivider : HDivider;

		
		
		//private var columndividers : Array = [];

		
		/**
		 * ObjectDivider divides a square into
		 * multiple parts horizontally or vertically.
		 * You can adjust them by dividers that are between them.
		 */
		public function ObjectDivider( skin : ComponentSkin = null, numrowdividers : int = 1, numcolumndividers : int = 0 )
		{
			super( skin ? skin : new ComponentSkin( ) );
			
			draw( );
			
			addRowDivider( numrowdividers );
			addColumnDivider( numcolumndividers );
		}

		
		
		public function addRowDivider( numdividers : int = 1, rowdividerheight : Number = 6 ) : void 
		{
			this.rowdividerheight = rowdividerheight;
			var i : int = 0;
			var d : HDivider;
			for (; i < numdividers; i++) {
				d = new HDivider( skin.width, rowdividerheight );
				d.addEventListener( Event.CHANGE, changeRowListener );
				d.addEventListener( Event.COMPLETE, changeRowListener );
				d.maxy = background.height - rowdividerheight;
				addChild( d );
			
				if( rowdividers.length > 0 ) {
					d.prev = rowdividers[ rowdividers.length - 1 ];
					d.prev.next = d;
				}
				rowdividers.push( d );
				d.index = rowdividers.length;
				rowobjects.push( null );
			}
			
			redraw( );
		}

		
		
		private function changeRowListener( e : Event ) : void 
		{
			if( e.type == Event.CHANGE ) {
				currentdivider = HDivider( e.currentTarget );
				if( currentdivider.index > 0 ) {
					currentchangeobjecta = rowobjects[ currentdivider.index - 1 ];
				}
				currentchangeobjectb = rowobjects[ currentdivider.index ];
				
				addEventListener( Event.ENTER_FRAME, enterChangeHandler );
			} else if (e.type == Event.COMPLETE ) {
				removeEventListener( Event.ENTER_FRAME, enterChangeHandler );
				enterChangeHandler( null );
			}
		}

		
		
		private function enterChangeHandler( e : Event ) : void 
		{
			if( currentchangeobjecta ) {
				var ay : Number = currentdivider.prev ? currentdivider.prev.y : 0;
				var aheight : Number = currentdivider.prev ? ((currentdivider.prev.y + 6) + currentdivider.y ) : currentdivider.y;
				
				currentchangeobjecta.y = ay;
				currentchangeobjecta.height = aheight;
			}
			
			if( currentchangeobjectb ) {
				var by : Number = currentdivider.next ? currentdivider.next.y + 6 : currentdivider.y + 6; // currentdivider.y + 6
				var bheight : Number = currentdivider.next ? ((currentdivider.next.y + 6) + currentdivider.y ) : currentdivider.maxy - (currentdivider.y );

				currentchangeobjectb.y = by;
				currentchangeobjectb.height = bheight;
			}
		}

		
		
		public function addColumnDivider( columndividerwidth : Number = 6 ) : void 
		{
			this.columndividerwidth = columndividerwidth;
		}

		
		
		private function draw() : void 
		{
			addChild( background = new Shape( ) );
			
			redraw( );
		}

		
		
		private function redraw() : void 
		{
			background.graphics.clear( );
			background.graphics.beginFill( skin.backgroundcolor, skin.backgroundalpha );
			background.graphics.drawRect( 0, 0, skin.width, skin.height );
			background.graphics.endFill( );
			
			var n : Number = skin.height / (rowdividers.length + 1);
			var i : int = 0;
			for (; i < rowdividers.length; i++) {
				var d : HDivider = HDivider( rowdividers[i] );
				d.y = (n * (i + 1)) - (rowdividerheight * .5);
			}
			enterChangeHandler( null );
		}

		
		
		override public function addChildAt( child : DisplayObject, index : int) : DisplayObject 
		{
			var n : Number = skin.height / (rowdividers.length + 1);
			child.y = ( index == 0 ) ? 0 : (n * index);
			child.height = ( n ) - ( rowdividerheight * .5 );
			addChild( child );
			rowobjects[ index ] = child;
			return child;
		}

		
		
		override public function set height(value : Number) : void {
			skin.height = value;
			redraw();
		}

		
		
		override public function set width(value : Number) : void {
			skin.width = value;
			redraw();
		}
	}
}
